#include "../core/engine/interface.h"
#include "frame.h"

namespace sleek
{
    namespace gui
    {
        frame::frame(core::engine::interface *m) : mom(m)
        {
            parent = 0;
            show = true;
            isActive = false;
            ft2 = mom->getInternalFont();
        }
        frame::~frame()
        {
            for(core::u32 i = 0; i<child.size(); ++i)
            {
                delete child[i];
                child[i] = 0;
            }
            child.clear();
            if(ft2) delete ft2;
        }
        void frame::addChild(frame *i)
        {
            child.push_back(i);
            i->relative = i->absolute;
            i->absolute = absolute+relative;
            i->parent = this;
        }
        void frame::removeChild(frame *i)
        {
            int x = child.linear_search(i);
            if(x != -1) removeChild(x);
        }
        void frame::setFont(font *i)
        {
            ft2 = i;
            i->make(text);
            UpdateFontPos();
        }
        void frame::setParent(frame *i)
        {
            parent = i;
            i->addChild(this);
        }
        void frame::setText(core::os::stringc t)
        {
            text = t;
            if(ft2) ft2->make(t);
        }
        frame* frame::getParent()
        {
            return parent;
        }
        frame* frame::getChild(core::s32 i)
        {
            return child[i];
        }
        void frame::removeChild(core::s32 i)
        {
            child.erase(i);
        }
        void frame::removeAll()
        {
            child.clear();
        }
        void frame::setVisible(bool i)
        {
            show = i;
        }
        bool frame::isVisible()
        {
            return show;
        }
        font* frame::getFont()
        {
            return ft2;
        }
        core::os::stringc frame::getText()
        {
            return text;
        }
        void frame::move(core::math::vector2di p)
        {
            if(parent) relative = p;
            else absolute = p;
            UpdateAbsolutePosition();
            UpdateFontPos();
        }
        bool frame::manage(core::device::input* e)
        {
            for(core::u32 i = 0; i<child.size(); ++i)
                if(child[i]->manage(e)) return true;
            return false;
        }
        void frame::setScale(core::math::vector2di i)
        {
            box.setSizeFromUpperLeft(i);
        }
        core::math::aabbox2di frame::getBoundingBox()
        {
            return box;
        }
        core::math::vector2di frame::getScale()
        {
            return box.getSize();
        }
        void frame::render()
        {
            if(!show) return;
            mom->getTheme()->drawFrame(this);
            renderChild();
        }
        void frame::renderChild()
        {
            for(core::u32 i = 0; i<child.size(); ++i)
                child[i]->render();
        }
        void frame::UpdateAbsolutePosition()
        {
            if(parent) absolute = parent->absolute+parent->relative;

            core::math::vector2di size = box.getSize();
            box.setUpperLeft(absolute + relative);
            box.setSizeFromUpperLeft(size);
            UpdateChildPosition();
        }
        void frame::UpdateChildPosition()
        {
            for(core::u32 i = 0; i<child.size(); ++i)
            {
                child[i]->absolute = absolute;
                child[i]->UpdateAbsolutePosition();
                child[i]->UpdateFontPos();
            }
        }
        void frame::UpdateFontPos()
        {
            textpos = absolute+relative;
            textpos += box.getCenter();
            textpos.width -= ft2->getBoxSize().width/2;
            textpos.height -= ft2->getBoxSize().height/2;
        }
    }
}
